package models

import (
	"godemo/dao"
	"time"
)

type Vote struct {
	Id         int       `json:"id"`
	UserId     int       `json:"user_id"`
	PlayerId   int       `json:"player_id"`
	CreateTime time.Time `json:"create_time"`
	UpdateTime time.Time `json:"update_time"`
}

func (Vote) TableName() string {
	return "vote"
}

func AddVote(userId int, playerId int) (int, error) {
	vote := Vote{UserId: userId, PlayerId: playerId, CreateTime: time.Now(), UpdateTime: time.Now()}
	err := dao.Db.Save(&vote).Error
	return vote.Id, err
}

func FindVote(userId int, playerId int) ([]Vote, error) {
	votes := []Vote{}
	err := dao.Db.Where("user_id = ? AND player_id = ?", userId, playerId).Find(&votes).Error
	return votes, err
}
